﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Possan.Localization;
using System.Globalization;

namespace Loot
{
	public partial class Editor : Form
	{
		List<string> selectedLocales;
		public string Filename { get; set; }
		string selectedNamespace;
		XmlLibraryProvider xmlprovider;
		ILibraryProvider library;

		void bindFiles()
		{
			listBox1.BeginUpdate();
			listBox1.Items.Clear();
			List<string> nss = library.GetNamespaces();
			foreach (string ns in nss)
			{
				listBox1.Items.Add(ns);
			}
			listBox1.EndUpdate();
		}

		bool bindingLocales = false;

		void bindLocales()
		{
			List<string> filelocales = library.GetLocales();

			_languageDropdownItem noitem = new _languageDropdownItem("", "-- None --");

			bindingLocales = true;

			cbLang1.BeginUpdate();
			cbLang2.BeginUpdate();
			cbLang3.BeginUpdate();

			cbLang1.Items.Clear();
			cbLang1.Items.Add(noitem);
			cbLang1.SelectedIndex = 0;
	
			cbLang2.Items.Clear();
			cbLang2.Items.Add(noitem);
			cbLang2.SelectedIndex = 0;

			cbLang3.Items.Clear();
			cbLang3.Items.Add(noitem);
			cbLang3.SelectedIndex = 0;

			for (int j = 0; j < filelocales.Count; j++)
			{
				_languageDropdownItem item = new _languageDropdownItem(filelocales[j], filelocales[j]);
				try
				{
					CultureInfo ci = new CultureInfo(item.ID);
					item.Label = ci.EnglishName;
				}
				catch (Exception w)
				{
				}

				cbLang1.Items.Add(item);
				if (selectedLocales.Count > 0)
					if (selectedLocales[0] == item.ID)
						cbLang1.SelectedIndex = j + 1;

				cbLang2.Items.Add(item);
				if (selectedLocales.Count > 1)
					if (selectedLocales[1] == item.ID)
						cbLang2.SelectedIndex = j + 1;

				cbLang3.Items.Add(item);
				if (selectedLocales.Count > 2)
					if (selectedLocales[2] == item.ID)
						cbLang3.SelectedIndex = j + 1;

			}

			cbLang1.EndUpdate();
			cbLang2.EndUpdate();
			cbLang3.EndUpdate();
	
			bindingLocales = false;
		}

		void bindNamespace()
		{
			string oldsel = "";
			if (listView1.SelectedItems.Count > 0)
				oldsel = listView1.SelectedItems[0].Tag.ToString();

			listView1.BeginUpdate();
			listView1.Columns.Clear();

			ColumnHeader chid = new ColumnHeader();
			chid.Text = "ID";
			listView1.Columns.Add(chid);
			foreach (string locale in selectedLocales)
			{
				if (!string.IsNullOrEmpty(locale))
				{
					CultureInfo ci = new CultureInfo(locale);
					ColumnHeader ch = new ColumnHeader();
					ch.Text = ci.EnglishName;
					listView1.Columns.Add(ch);
				}
			}

			listView1.Items.Clear();
			List<string> ids = library.GetIDs(selectedNamespace);
			foreach (string id in ids)
			{
				ListViewItem itm = new ListViewItem();
				itm.Tag = id;
				itm.Text = id;
				for (int j = 0; j < selectedLocales.Count; j++)
				{
					string locale = selectedLocales[j];
					if (!string.IsNullOrEmpty(locale))
					{
						string text = library.TranslatePhraseByID(selectedNamespace, id, "** Not translated **", new CultureInfo(locale));
						itm.SubItems.Add(new ListViewItem.ListViewSubItem(itm, text));
					}
				}
				listView1.Items.Add(itm);
				itm.Selected = (id == oldsel);
			}

			listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
			listView1.EndUpdate();
		}

		public Editor()
		{
			InitializeComponent();
			Load += new EventHandler(Editor_Load2);
		}

		private void Editor_Load2(object sender, EventArgs e)
		{
			Text = Filename;

			xmlprovider = new XmlLibraryProvider();
			xmlprovider.ImportXML(Filename);
			library = xmlprovider;

			bindFiles();

			selectedLocales = new List<string>();
			List<string> filelocales = library.GetLocales();
			if (filelocales.Count > 0)
				selectedLocales.Add(filelocales[0]);
			if (filelocales.Count > 1)
				selectedLocales.Add(filelocales[1]);
			if (filelocales.Count > 2)
				selectedLocales.Add(filelocales[2]);

			bindLocales();

			if (listBox1.Items.Count > 0)
				listBox1.SelectedIndex = 0;

			treeView1_AfterSelect(null, null);
		}

		private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (e != null && e.Node != null )
			{
				selectedNamespace = e.Node.Text;
			}
			bindNamespace();
		}

		void editSelected()
		{
			if (listView1.SelectedItems.Count > 0)
			{
				Phrase p = new Phrase();
				p.Provider = library;
				p.selectedNamespace = selectedNamespace;
				p.selectedLocales = selectedLocales;
				p.selectedId = listView1.SelectedItems[0].Tag.ToString();
				p.ShowDialog();
				xmlprovider.ExportXML(Filename);
				bindNamespace();
			}
		}

		private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			editSelected();
		}

		private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			selectedNamespace = listBox1.Items[listBox1.SelectedIndex].ToString();
			bindNamespace();
		}

		private void buEditLanguages_Click(object sender, EventArgs e)
		{
			Locales l = new Locales();
			l.Provider = library;
			l.ShowDialog();
			bindLocales();
			bindNamespace();
			xmlprovider.ExportXML(Filename);
		}

		private void cbLang1_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (bindingLocales)
				return;

			_languageDropdownItem item = (_languageDropdownItem)cbLang1.Items[cbLang1.SelectedIndex];
			if (selectedLocales.Count > 0)
				selectedLocales[0] = item.ID;
			else
				selectedLocales.Add(item.ID);
			
			bindNamespace();
		}

		private void cbLang2_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (bindingLocales)
				return;
			
			_languageDropdownItem item = (_languageDropdownItem)cbLang2.Items[cbLang2.SelectedIndex];
			if (selectedLocales.Count > 1)
				selectedLocales[1] = item.ID;
			else
				selectedLocales.Add(item.ID);
			
			bindNamespace();
		}

		private void cbLang3_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (bindingLocales)
				return;

			_languageDropdownItem item = (_languageDropdownItem)cbLang3.Items[cbLang3.SelectedIndex];
			if (selectedLocales.Count > 2)
				selectedLocales[2] = item.ID;
			else
				selectedLocales.Add(item.ID);
			
			bindNamespace();
		}

		private void listView1_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
				editSelected();
		}

	}

	class _languageDropdownItem
	{
		public string ID { get; set; }

		public string Label { get; set; }

		public _languageDropdownItem(string id, string lab)
		{
			ID = id;
			Label = lab;
		}
		public override string ToString()
		{
			return Label;
		}
	}
}
